package com.kitmobile.view;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

import com.kitmobile.R;
import com.kitmobile.data.KITDatabase;
import com.kitmobile.model.KITContact;

public class KITList extends ListActivity {
	KITDatabase db;
	Cursor cursor;
	Button addContactButton;
	
	public static final int REQUEST_ADD_REMINDER = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kitlist);
        
        db = new KITDatabase(this);
        db.open();
        
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(KITContact.TableData.TABLE_NAME);
        cursor = db.query(builder);
        startManagingCursor(cursor);
        
        ListAdapter adapter = new SimpleCursorAdapter(this, 
        											  R.layout.kitlist_row, 
        											  cursor,
        											  new String[]{KITContact.TableData.CONTACT_ID}, 
        											  new int[]{R.id.kitlist_row_contactNameLabel});
        this.setListAdapter(adapter);
        
        addContactButton = (Button)findViewById(R.id.kitlist_addContactButton);
    }
    
    public void addContact(View view) {
    	Intent contactPicker = new Intent(this, KITReminder.class);
    	startActivityForResult(contactPicker, REQUEST_ADD_REMINDER);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == RESULT_OK) {
    		switch(requestCode) {
    		case REQUEST_ADD_REMINDER:
    			
    			break;
    		}
    	}
    }
    
    @Override
    public void onDestroy() {
    	db.close();
    	super.onDestroy();
    }
}